/* -----------------------------------------------------------------------
 * Paul Cohen
 * CS 362, Summer 2014
 * Assignment 3
 *
 * unittest1.c - testing Dominion's shuffle() function
 * 
 * -----------------------------------------------------------------------
 */

#include "unittest1.h"
#include "dominion.h"
#include "dominion_helpers.h"
#include "rngs.h"
#include <string.h>
#include <stdio.h>
#include <assert.h>

#define SHUFFLE_TIMES 100
#define MAX_DECK_CARDS 10

int unit_test1()
{
    int i, j;
    int seed = 1000;
    int numPlayers = 2;
    int p = 0;
    int k[10] = {adventurer, council_room, feast, gardens, mine, remodel, smithy, village, baron, great_hall};
    int oldDeckOrder[MAX_DECK_CARDS];
    int newDeckOrder[MAX_DECK_CARDS];
    int diffCard = 0;
    int sameCard = 0;
    int errors = 0;
    int successes = 0;
    float percentMatch = 0;
    struct gameState g;

    printf("\nUnit Test 1: testing shuffle()\n------------\n");

    // initialize new game
    if(initializeGame(numPlayers, k, seed, &g) == -1) 
    {
        printf("Error initializing game.\n");
        return -1;
    }

    for (i = 0; i < MAX_DECK_CARDS; i++)
    {
        g.deck[p][i] = Random() * 26;
    }

    for (j = 0; j < SHUFFLE_TIMES; j++)
    {
        diffCard = 0;
        sameCard = 0;
        // store deck order in oldDeckOrder
        for (i = 0; i < MAX_DECK_CARDS; i++)
            oldDeckOrder[i] = g.deck[p][i];
               
        if (shuffle(p, &g) == -1)
        {
            printf("Error while shuffling.\n");
            errors++;
            continue;
        }
       
        // store shuffled deck in newDeckOrder
        for (i = 0; i < MAX_DECK_CARDS; i++)
        {
            newDeckOrder[i] = g.deck[p][i];
        }
        
        // compare oldDeckOrder and newDeckOrder 
        for (i = 0; i < MAX_DECK_CARDS; i++)
        {
            if (oldDeckOrder[i] != newDeckOrder[i])
                diffCard++;
            else
                sameCard++;
        }
       
        percentMatch = ((float)sameCard / MAX_DECK_CARDS) * 100;
       
        if (percentMatch > 50)
        { 
            errors++;
        }
        else
            successes++;
           
    }

    printf("shuffle() tests completed!\n");
    printf("%d out of %d shuffles actually shuffled well (i.e. over 50%% of cards are in different positions).\n", successes, SHUFFLE_TIMES);

    return 0;
}
